package com.erlin.platform.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.erlin.common.exception.ElException;
import com.erlin.common.utils.MessageUtils;
import com.erlin.platform.constant.user.UserRoleEnum;
import com.erlin.platform.entity.ElRole;
import com.erlin.platform.entity.UserRoleMid;
import com.erlin.platform.mapper.UserRoleMidMapper;
import com.erlin.platform.service.IElRoleService;
import com.erlin.platform.service.IUserRoleMidService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;
import java.util.Optional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author alin
 * @since 2021-05-18
 */
@Service
@Transactional
public class UserRoleMidServiceImpl extends ServiceImpl<UserRoleMidMapper, UserRoleMid> implements IUserRoleMidService {
    @Autowired
    private IElRoleService elRoleService;

    @Override
    public void insert(Long userId, UserRoleEnum roleEnum) {
        //查询对应枚举类角色是否存在
        ElRole elRole = Optional.ofNullable(elRoleService.getOne(Wrappers.<ElRole>lambdaQuery().eq(ElRole::getClass, roleEnum.getCode())
                .eq(ElRole::getUserClas, roleEnum.getUserClas()))).orElseThrow(() -> new ElException(MessageUtils.getMessage("user.role.init.error")));
        save(new UserRoleMid(userId,elRole.getId()));
    }
}
